import java.util.*;

public class Test {



    //字⺟异位词分组
    public List<List<String>> groupAnagrams(String[] strs){
        Map<String ,List<String>> hash = new HashMap<>();

        for (String s: strs) {
            char[] tmp = s.toCharArray();
            Arrays.sort(tmp);
            String key = new String(tmp);

            if (!hash.containsKey(key)){
                hash.put(key,new ArrayList<>());
            }
            hash.get(key).add(s);
        }
        return new ArrayList<>(hash.values());
    }




    //存在重复元素II
    public boolean containsNearbyDuplicate(int[] nums,int k){
        Map<Integer,Integer> hash = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (hash.containsKey(nums[i])){
                if (i-hash.get(nums[i]) <= k) return true;
            }
            hash.put(nums[i],i);
        }
        return false;
    }




    //存在重复元素I
    public boolean containsDuplicate(int[] nums){
        Set<Integer> hash = new HashSet<>();
        for (int x:nums) {
            if (hash.contains(x)) return true;
            hash.add(x);
        }
        return false;
    }




    //判断是否互为字符重排
    public boolean CheckPermutation(String s1,String s2){
        if (s1.length() != s2.length()) return false;

        int[] hash = new int[26];
        for (int i = 0; i < s1.length(); i++) {
            hash[s1.charAt(i)-'a'] ++;
        }

        for (int i = 0; i < s2.length(); i++) {
            hash[s2.charAt(i)-'a']--;
            if (hash[s2.charAt(i)-'a'] < 0) return false;
        }
        return true;
    }



    //两数之和
    public int[] twoSum(int[] nums,int target){
        Map<Integer,Integer> hash = new HashMap<>();//值 下标

        for (int i = 0; i < nums.length; i++) {
            int x = target-nums[i];

            if (hash.containsKey(x)){
                return new int[] {i,hash.get(x)};
            }
            hash.put(nums[i],i);
        }
        return new int[] {-1,-1};
    }





}
